class Entry {
  constructor(next, data) {
    this.next = next;
    this.data = data;
  }
}

class List {
  constructor() {
    this.head = new Entry(null, null);
    this.end = new Entry(null, null);
    this.length = 0;

    /** 链表末尾添加元素 */
    this.push = (data) => {
      const newentry = new Entry(null, data);
      if (this.head.data) {
        this.end.next = newentry;
        this.end = newentry;
      } else {
        this.head = newentry;
        this.end = newentry;
      }
      this.length += 1;
    };

    /** 删除第一个元素 */
    this.shift = () => {
      if (this.head.next) {
        this.head = this.head.next;
        this.length -= 1;
      } else if (this.head.data) {
        this.head.data = null;
        this.end.data = null;
        this.length -= 1;
      }
    };

    /** 获取第一个元素的值 */
    this.front = () => {
      if (this.head.data) {
        return this.head.data;
      }
      return null;
    };
  }
}

module.exports.List = List;
